/*
 * @Description: 指点飞行的点
 * @Date: 2022-03-31 17:53:21
 * @Author: yeming
 */

import { fromLonLat } from 'ol/proj.js'
import { Point } from '../model/point.js'
import { Circle, Circle3d } from '../model/circle.js'
import { setOlStyleCircle } from '../../common/olStyle.js'
import { removeFeatureAndDeleteHashMap, removeAllFeatureAndDeleteHashMap } from '../common/removeFeature.js'

const AIRLINE_GIVE_POINT = './static/olcsMap/img/give_point_to_fly.png'
const DEFAULT_CIRCLE_STYLE = { 'fillColor': 'rgba(0, 153, 255, 0.1)', 'strokeColor': 'rgba(0, 153, 255, 1)', 'strokeWidth': 2 }

class GivePointFly {
  constructor(ol2d, ol3d, pointHashMap, circleHashMap, vectorSource) {
    this.ol2d = ol2d
    this.pointHashMap = pointHashMap
    this.circleHashMap = circleHashMap
    this.vectorSource = vectorSource
    this.entities = ol3d.entities
  }

  /**
   * @desc: 指点飞行模式在飞行之前打的点（有且只有一个，第二次点击替换上一次）
   * @param {Number} sessionId
   * @param {Array} latlng
   * @param {String} image
   * @param {Number} radius
   * @param {Object} style
   */
  addGivePointToFly (sessionId, latlng, image = AIRLINE_GIVE_POINT, radius, style) {
    image = image != '' && image ? image : AIRLINE_GIVE_POINT

    let currentLine = this.pointHashMap.get(sessionId)
    if (currentLine == undefined) {
      let point = new Point(this.ol2d, this.vectorSource)
      point.addSinglePoint(sessionId, latlng, 8, 0, image, false, null, true)
      this.pointHashMap.set(sessionId, { 'item2d': point, 'item3d': null }) // 已做3d同步 rend3d = true item3d = null

      if (radius > 0) this.addGivePointCircle(sessionId, latlng, 2, 1, radius, style, false)
    } else {
      this.updateGivePointPosition(sessionId, latlng, radius, style)
    }
  }

  /**
   * @desc: 更新指点飞行的点
   * @param {Number} sessionId
   * @param {Array} latlng
   */
  updateGivePointPosition (sessionId, latlng, radius, style) {
    let currentPoint = this.pointHashMap.get(sessionId).item2d
    let coordinate = fromLonLat(latlng)
    let feature = currentPoint.feature
    if (feature != null) feature.getGeometry().setCoordinates(coordinate)

    if (radius > 0) this.updateGivePointCircle(sessionId, latlng, radius, style)
  }

  /**
   * @desc: 删除指点飞行的点
   * @param {Number} sessionId
   */
  delGivePointToFly (sessionId) {
    removeFeatureAndDeleteHashMap(sessionId, this.pointHashMap, this.vectorSource)
    removeFeatureAndDeleteHashMap(sessionId, this.circleHashMap, this.vectorSource, this.entities)
  }

  /**
   * @desc: 删除所有指点飞行的点
   */
  delAllGivePoint () {
    removeAllFeatureAndDeleteHashMap(this.pointHashMap, this.vectorSource)
    removeAllFeatureAndDeleteHashMap(this.circleHashMap, this.vectorSource, this.entities)
  }

  /**
   * @desc: 渲染circle
   * @param {Number} sessionId
   * @param {Number} type 1-单线飞行和机载航线中的航点 2-指点飞行的点 3-自定义点 4-吊舱视野位置区域 5-禁飞区 6-电子围栏
   * @param {Array} latlng
   * @param {Number} isHover
   * @param {Number} radius
   * @param {Object} style
   * @param {Number} rend3d
   */
  addGivePointCircle (sessionId, latlng, type, isHover, radius, style, rend3d) {
    let circle = new Circle(this.ol2d, this.vectorSource)
    circle.addCircle2(sessionId, latlng, type, isHover, radius, style, rend3d)

    let position = new Cesium.Cartesian3.fromDegrees(latlng[0], latlng[1], latlng[2] || 80)
    let circle3d = new Circle3d(this.entities, 'givePointCircle3d_' + sessionId, type, latlng, radius, null)
    circle3d.position = position

    this.circleHashMap.set(sessionId, { 'item2d': circle, 'item3d': circle3d })
    return sessionId
  }

  /**
   * @desc: 更新circle
   * @param {Number} sessionId
   * @param {Array} latlng
   * @param {Number} radius
   * @param {Object} style
   */
  updateGivePointCircle (sessionId, latlng, radius, style) {
    let currentCircle = this.circleHashMap.get(sessionId).item2d
    currentCircle.updateCircle2(sessionId, latlng, 2, 1, radius, style, false, this.circleHashMap)
  }
}

export default GivePointFly
